﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OF.Component.BLL;
using OF.Component.Model;
using OF.Utility;
using OpSystem.common;

namespace OpSystem.Manage.authority
{
    public partial class AuthorityRoleMenuRelaEdit : BasePage
    {
        protected int intRoleID {
            get {
                return RequestUtility.GetQueryInt("id", 1);
            }
        }

        protected int intSystemID
        {
            get
            {
                return RequestUtility.GetQueryInt("sysid", 1);
            }
        }

        protected string strMessage = "";
        protected string strHtml = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindMenuList();

            }
        }

        private void BindMenuList()
        {
            int count = 0;
            string strMenu = "";
            string strSystem = "";
            IList <AuthoritySystemInfo> sysList = AuthoritySystemOperate.QueryList(a => a.IsActive == 1);
            AuthorityRoleMenuRelaOperate.GetAuthorityToString(intRoleID,out strMenu,out strSystem);

            StringBuilder sb = new StringBuilder();
            int i = 0;
            foreach (var item in sysList)
            {
                i++;
                sb.AppendFormat("<div class=\"panel panel-info\"><div class=\"panel-heading\"><h4 class=\"panel-title\"><input type=\"checkbox\" {1}/><a data-toggle=\"collapse\" data-parent=\"#accordion\" href=\"#collapse{0}\">", i, checkSystem(strSystem,item.SystemID));
                sb.AppendFormat("{0}<span class=\"glyphicon glyphicon-plus\" style=\"float: right; \"></span></a></h4></div>", item.SystemName);
                sb.AppendFormat("<div id=\"collapse{0}\" class=\"panel-collapse collapse\"><div class=\"panel-body\">",i);

                IList<AuthorityMenuInfo> list = AuthorityMenuOperate.Search(out count, 1, 9999, "1=1 and IsActive=1 AND SystemID=" + item.SystemID + "", "OrderNum", false);

                var ParentList = from AuthorityMenu in list
                                 where AuthorityMenu.ParentID == 0
                                 select new { MenuName = AuthorityMenu.MenuName, MenuID = AuthorityMenu.MenuID, MenuUrl = AuthorityMenu.MenuUrl };
                foreach (var pv in ParentList)
                {
                    sb.AppendFormat("<p class=\"panel-mainhead\"><input type = \"checkbox\" class=\"ckb\" value=\"{0}\" {3}/>{1}</p><ul class=\"list-inline\" data-parent=\"{2}\">", pv.MenuID, pv.MenuName, pv.MenuID, checkAuthority(strMenu, pv.MenuID));
                    var ChildList = from AuthorityMenu in list
                                    where AuthorityMenu.ParentID == pv.MenuID
                                    select new { MenuName = AuthorityMenu.MenuName, MenuID = AuthorityMenu.MenuID, MenuUrl = AuthorityMenu.MenuUrl };

                    foreach (var cv in ChildList)
                    {
                        sb.AppendFormat("<li><input type=\"checkbox\"  class=\"ckb\" value=\"{0}\" {2}  >{1}</li>", cv.MenuID, cv.MenuName, checkAuthority(strMenu, cv.MenuID));

                    }
                    sb.AppendLine("</ul>");
                }
                sb.AppendLine("</div></div></div>");
            }
            strHtml = sb.ToString();

        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string MessageInfo = "信息修改成功！";
         
          //  strMessage = MessageDivInfo(MessageInfo, "");
        }

        private string checkAuthority(string strMenu, int MenuID)
        {
            string strRun = "";
            if (strMenu.IndexOf("," + MenuID.ToString() + ",") > -1)
            {
                strRun = "checked=\"true\"";
            }

            return strRun;
        }

        private string checkSystem(string strSystem, int sysId)
        {
            string strRun = "";
            if (strSystem.IndexOf("," + sysId.ToString() + ",") > -1)
            {
                strRun = "checked=\"true\"";
            }

            return strRun;
        }
    }
}
